package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.UserAddr;
import com.lam.mall.mapper.UserAddrMapper;
import com.lam.mall.param.UserAddrParam;
import com.lam.mall.service.UserAddrService;

/**
 * 用户配送地址Service业务层处理
 * 
 * @author admin
 * @date 2023-04-26
 */
@Service
public class UserAddrServiceImpl  implements UserAddrService {

    @Autowired
    private UserAddrMapper userAddrMapper;

    /**
     * 查询用户配送地址
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public UserAddr selectById(String id) {

        return userAddrMapper.selectById(id);
    }

    /**
     * 查询用户配送地址列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<UserAddr> selectList(UserAddrParam param) {
    
        QueryWrapper<UserAddr> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getReceiver()), "receiver", param.getReceiver());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMobile()), "mobile", param.getMobile());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(Objects.nonNull(param.getDefaultAddr()), "default_addr", param.getDefaultAddr());

        return userAddrMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户配送地址
     * 
     * @param userAddr 
     * @return 
     */
    @Override
    @Transactional
    public int insert(UserAddr userAddr) {
    
        return userAddrMapper.insert(userAddr);
    }

    /**
     * 修改用户配送地址
     * 
     * @param userAddr 
     * @return 
     */
    @Override
    @Transactional
    public int update(UserAddr userAddr) {
    
        return userAddrMapper.updateById(userAddr);
    }

    /**
     * 删除用户配送地址信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return userAddrMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
	public UserAddr getDefaultAddr(String userId) {
    	QueryWrapper<UserAddr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("default_addr", CommonConstant.YES1);
        return userAddrMapper.selectOne(queryWrapper);
	}

	@Override
	@Transactional
	public void updateDefaultAddr(String addrId, String userId) {
		userAddrMapper.removeDefaultUserAddr(userId);
		userAddrMapper.setDefaultUserAddr(addrId,userId);
	}

	@Override
	@Cacheable(cacheNames = "UserAddrVo", key = "#userId+':'+#addrId")
	public UserAddr getUserAddrCache(String addrId, String userId) {
		
		QueryWrapper<UserAddr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("id", addrId);
        UserAddr userAddr = userAddrMapper.selectOne(queryWrapper);
		if (userAddr == null) {
			return getDefaultAddr(userId);
		}
		return userAddr;
	}
	
    @Override
	@CacheEvict(cacheNames = "UserAddrVo", key = "#userId+':'+#addrId")
    public void removeUserAddrCache(String addrId, String userId) {

    }
	
}
